package com.jf.utils;

import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.Random;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User:22954
 * Date:2024-08-20
 * Time:1:08
 */
public class StarSet {
    private final Star[] smallStars;
    private final Star[] mediumStars;
    private final Star[] bigStars;

    private final ArrayList<Star> twinkleStars;

    public StarSet() {
        smallStars = new Star[130];
        mediumStars = new Star[90];
        bigStars = new Star[50];
        twinkleStars = new ArrayList<>();

        Position[] positions = new Position[270];
        Random rand = new Random();
        LinkedHashSet<Position> positionSet = new LinkedHashSet<>();
        for (int i = 0; i < positions.length; i++) {
            positions[i] = new Position(rand.nextInt(0, 1280), rand.nextInt(0, 720));
            while (true) {
                if (positionSet.contains(positions[i])) {
                    positions[i] = new Position(rand.nextInt(0, 1280), rand.nextInt(0, 720));
                } else {
                    positionSet.add(positions[i]);
                    break;
                }
            }
        }
        for (int i = 0; i < smallStars.length; i++) {
            Position position = positionSet.getFirst();
            positionSet.removeFirst();
            smallStars[i] = new Star(position.x, position.y, Star.Grade.SMALL);
            if (smallStars[i].isTwinkle()) twinkleStars.add(smallStars[i]);
        }
        for (int i = 0; i < mediumStars.length; i++) {
            Position position = positionSet.getFirst();
            positionSet.removeFirst();
            mediumStars[i] = new Star(position.x, position.y, Star.Grade.MEDIUM);
            if (mediumStars[i].isTwinkle()) twinkleStars.add(mediumStars[i]);
        }
        for (int i = 0; i < bigStars.length; i++) {
            Position position = positionSet.getFirst();
            positionSet.removeFirst();
            bigStars[i] = new Star(position.x, position.y, Star.Grade.BIG);
            if (bigStars[i].isTwinkle()) twinkleStars.add(bigStars[i]);
        }
    }

    public void twinkle() {
        for (Star twinkleStar : twinkleStars) {
            twinkleStar.Fading();
        }
    }

    public void draw(Graphics g) {
        for (Star star : smallStars) {
            star.draw(g);
        }
        for (Star star : mediumStars) {
            star.draw(g);
        }
        for (Star star : bigStars) {
            star.draw(g);
        }
    }
}
